package test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class ArrayListVSLinkedList {
    static Random r = new Random();
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        LinkedList<Integer> linkedList = new LinkedList<>();
        int n=1000_000;
        System.out.println("staerr");
        Thread array = new Thread(() -> {
            test(arrayList, n, "ArrayList");
        });
        Thread linked = new Thread(() -> {
            test(linkedList, n, "LinkedList");
        });
        array.start();
        linked.start();
    }

    public static void test(ArrayList<Integer> arrayList, int n, String name) {
        long start = System.currentTimeMillis();
        System.out.println(name + "尾部插入" + n + "个元素:" + insertTail(arrayList, n, name + "尾部插入")+ "ms");
        System.out.println(name + "遍历" + n + "个元素:" + every(arrayList, name + "遍历")+ "ms");
        System.out.println(name + "尾部删除" + n + "个元素:" + removeArrayLast(arrayList, n, name + "尾部删除")+ "ms");
        System.out.println(name + "头部插入" + n + "个元素:" + insertArrayHead(arrayList, n, name + "头部插入")+ "ms");
        System.out.println(name + "头部删除" + n + "个元素:" + removeArrayHead(arrayList, n, name + "头部删除")+ "ms");
        long end = System.currentTimeMillis();
        System.out.println(name + "耗时：" + (end - start) + "ms");
    }

    public static void test(LinkedList<Integer> linkedList, int n, String name) {
        long start = System.currentTimeMillis();
        System.out.println(name + "尾部插入" + n + "个元素:" + insertTail(linkedList, n, name + "尾部插入")+ "ms");
        System.out.println(name + "遍历" + n + "个元素:" + every(linkedList, name + "遍历")+ "ms");
        System.out.println(name + "尾部删除" + n + "个元素:" + removeLinkedLast(linkedList, n, name + "尾部删除")+ "ms");
        System.out.println(name + "头部插入" + n + "个元素:" + insertLinkedHead(linkedList, n, name + "头部插入")+ "ms");
        System.out.println(name + "头部删除" + n + "个元素:" + removeLinkedHead(linkedList, n, name + "头部删除")+ "ms");
        long end = System.currentTimeMillis();
        System.out.println(name + "耗时：" + (end - start) + "ms");
    }

    public static long insertArrayHead(ArrayList<Integer> arrayList, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            arrayList.add(0, r.nextInt());
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long insertLinkedHead(LinkedList<Integer> linkedList, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            linkedList.addFirst(r.nextInt());
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    private static long removeLinkedHead(LinkedList<Integer> linkedList, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            linkedList.removeFirst();
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    private static long removeArrayHead(ArrayList<Integer> arrayList, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            arrayList.remove(0);
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long removeLinkedLast(LinkedList<Integer> list, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.removeLast();
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long removeArrayLast(ArrayList<Integer> list, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.remove(list.size()-1);
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long insertTail(List<Integer> list, int n, String name) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            list.add(r.nextInt());
            ProgressBar.printProgress(name, i, n);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public static long every(List<Integer> list, String name){
        long start = System.currentTimeMillis();
        int i = 0;
        for (Integer num : list) {
            ProgressBar.printProgress(name, i++, list.size());
        }
        long end = System.currentTimeMillis();
        return end - start;
    }
}
